/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package granjaWSPackage;

import bd.consulta;
import bd.usuario;
import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.db4o.query.Query;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;

/**
 *
 * @author jonathan
 */
@WebService(serviceName = "granjaWS")
public class granjaWS {

    /** This is a sample web service operation */
    @WebMethod(operationName = "hello")
    public String hello(@WebParam(name = "name") String txt) {
        return "Hello " + txt + " !";
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "listarClientes")
    public String listarClientes() { //usuario-pais;usuario2-pais2
        //TODO write your implementation code here:
        String retorno = "";
        consulta c = new consulta();
        ObjectSet result = null;
        try {       
            c.abrirBD();
            result = c.selectUsuarios();           
            if(result==null)return "";
            
            while(result.hasNext()) {
                // recorrer cada usuario                
                usuario us = (usuario) result.next();
                if(!retorno.equals(""))retorno += ";"; //agregar el punto y coma, si existia algo antes
                retorno += us.getUsername() + "-" + us.getGranja().getPais();                
            }
            System.out.println("enviando..:"+ retorno);         
            c.cerrarBD();
            
        } catch (Exception e) {
            System.out.println("[WS]error al enviar usuario-pais: " + e.getLocalizedMessage() + " , " + e.toString());
        }
        return retorno;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "listarPaises")
    public String listarPaises() { //pais1;pais2
        //TODO write your implementation code here:
        String paises = "Guatemala;Brazil;Canada";
        return paises;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "productosCliente")
    public String productosCliente(@WebParam(name = "user")
    String user) { //item-canitidad;item2-cantidad
        //TODO write your implementation code here:
           /*
     * Tipos de Productos
     * 1 - Zanahoria
     * 2 - Maiz
     * 3 - Vaca
     * 4 - Oveja
     * 5 - Duraznal
     * 6 - Naranja
     * 7 - Silla
     * 8 - Faro
     * 9 - Casa
     * 10- Granero
     */
        String retorno = "";
        consulta c = new consulta();
        usuario us = c.getUsuario(user);
        if(us != null){            
            int [] cant = us.getGranja().almacenados;
            int n = cant.length;
            for(int i=0; i<n; i++){
                switch(i){
                    case 0: retorno += "Zanahoria-" + cant[i] + ";";
                        break;
                    case 1: retorno += "Maiz-" + cant[i] + ";";
                        break;
                    case 2: retorno += "Vaca-" + cant[i] + ";";
                        break;
                    case 3: retorno += "Oveja-" + cant[i] + ";";
                        break;
                    case 4: retorno += "Duraznal-" + cant[i] + ";";
                        break;
                    case 5: retorno += "Naranja-" + cant[i] + ";";
                        break;                        
                }
            }
        }
        return retorno;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "listarProductos")
    public String listarProductos() {//item-precio;item2-precio2
        //TODO write your implementation code here:
        String retorno = "Zanahoria-1.00;Maiz-0.50;Vaca-50.00;Oveja-25.00;Casa-200.00;Granero-100.00;Durazno-5.00;Naranja-5.00;Silla-10.00;Faro-15.00";
        return retorno;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "TconsultarSaldo")
    public Float TconsultarSaldo(@WebParam(name = "user")
    String user) {
        //TODO write your implementation code here:
        float saldo = (float)500.00;
        return saldo;
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "TdisminuirSaldo")
    public String TdisminuirSaldo(@WebParam(name = "user")
    String user, @WebParam(name = "cantidad")
    float cantidad) {
        //TODO write your implementation code here:
        return "Done";
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "TaumentarSaldo")
    public String TaumentarSaldo(@WebParam(name = "user")
    String user, @WebParam(name = "cantidad")
    float cantidad) {
        //TODO write your implementation code here:
        return "Done";
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "TenviarItems")
    public String TenviarItems(@WebParam(name = "userOrigen")
    String userOrigen, @WebParam(name = "userDestino")
    String userDestino, @WebParam(name = "producto")
    String producto, @WebParam(name = "cantidad")
    int cantidad) {
        //TODO write your implementation code here:
        return "Done";
    }
}
